<template>
  <div>
    <h2 class="page-title">
      <el-icon><Cpu /></el-icon>
      装置管理
    </h2>
    <div class="card">
      <div class="table-operations">
        <el-button type="primary" :icon="Plus" @click="handleAddDevice">
          添加装置
        </el-button>
        <el-input
          placeholder="搜索装置"
          style="width: 300px"
          v-model="deviceSearch"
          clearable
        >
          <template #append>
            <el-button :icon="Search"></el-button>
          </template>
        </el-input>
      </div>

      <el-table
        :data="filteredDeviceList"
        stripe
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="80"
          align="center"
        ></el-table-column>
        <el-table-column prop="name" label="装置名称"></el-table-column>
        <el-table-column
          prop="type"
          label="类型"
          width="100"
          align="center"
        ></el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag
              v-if="scope.row.status === 'online'"
              type="success"
              effect="dark"
            >
              <el-icon><SuccessFilled /></el-icon>
              在线
            </el-tag>
            <el-tag v-else type="danger" effect="dark">
              <el-icon><WarningFilled /></el-icon>
              离线
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="pond"
          label="所属虾塘"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="installTime"
          label="安装时间"
          width="120"
        ></el-table-column>
        <el-table-column label="操作" width="250" fixed="right" align="center">
          <template #default="scope">
            <el-button
              size="small"
              :icon="DataLine"
              @click="handleViewData(scope.row)"
              >数据</el-button
            >
            <el-button
              size="small"
              type="primary"
              :icon="Edit"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="danger"
              :icon="Delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 20, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import { Cpu, SuccessFilled, WarningFilled } from "@element-plus/icons-vue";

export default {
  name: "DeviceList",
  setup() {
    const Plus = ElementPlusIconsVue.Plus;
    const Search = ElementPlusIconsVue.Search;
    const DataLine = ElementPlusIconsVue.DataLine;
    const Edit = ElementPlusIconsVue.Edit;
    const Delete = ElementPlusIconsVue.Delete;

    const deviceSearch = ref("");
    const loading = ref(false);
    const currentPage = ref(1);
    const pageSize = ref(10);
    const total = ref(0);

    const deviceList = ref([
      {
        id: 1,
        name: "溶解氧传感器#1",
        type: "传感器",
        status: "online",
        pond: "1号虾塘",
        installTime: "2023-02-28",
      },
      {
        id: 2,
        name: "pH传感器#1",
        type: "传感器",
        status: "online",
        pond: "1号虾塘",
        installTime: "2023-02-28",
      },
      {
        id: 3,
        name: "增氧机#1",
        type: "执行器",
        status: "online",
        pond: "1号虾塘",
        installTime: "2023-03-01",
      },
      {
        id: 4,
        name: "水温传感器#2",
        type: "传感器",
        status: "offline",
        pond: "2号虾塘",
        installTime: "2023-03-05",
      },
      {
        id: 5,
        name: "投饵机#1",
        type: "执行器",
        status: "online",
        pond: "3号虾塘",
        installTime: "2023-03-10",
      },
    ]);

    const filteredDeviceList = computed(() => {
      if (!deviceSearch.value) {
        return deviceList.value;
      }
      return deviceList.value.filter(
        (device) =>
          device.name.includes(deviceSearch.value) ||
          device.type.includes(deviceSearch.value) ||
          device.pond.includes(deviceSearch.value)
      );
    });

    const handleAddDevice = () => {
      ElMessage.info("添加装置功能");
    };

    const handleViewData = (row) => {
      ElMessage.info(`查看 ${row.name} 数据`);
    };

    const handleEdit = (row) => {
      ElMessage.info(`编辑装置: ${row.name}`);
    };

    const handleDelete = (row) => {
      ElMessageBox.confirm(`确定要删除装置 "${row.name}" 吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deviceList.value = deviceList.value.filter(
            (device) => device.id !== row.id
          );
          ElMessage.success("删除成功");
        })
        .catch(() => {});
    };

    const handleSizeChange = (val) => {
      pageSize.value = val;
      currentPage.value = 1;
    };

    const handleCurrentChange = (val) => {
      currentPage.value = val;
    };

    onMounted(() => {
      total.value = deviceList.value.length;
    });

    return {
      Cpu,
      SuccessFilled,
      WarningFilled,
      Plus,
      Search,
      DataLine,
      Edit,
      Delete,
      deviceSearch,
      loading,
      deviceList,
      filteredDeviceList,
      currentPage,
      pageSize,
      total,
      handleAddDevice,
      handleViewData,
      handleEdit,
      handleDelete,
      handleSizeChange,
      handleCurrentChange,
    };
  },
};
</script>

<style scoped>
.page-title {
  font-size: 20px;
  margin-bottom: 20px;
  color: #1b9a7c;
  padding-bottom: 10px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.page-title .el-icon {
  font-size: 24px;
}

.table-operations {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}

.card {
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}
</style>
